package com.example.springboot3.context;

import com.example.springboot3.pojo.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @author 张琪
 * @date 2024-02-23 14:15
 */
public class RequestContext {
    public static HttpServletRequest getCurrentRequest() {
        // RequestContextHolder 可以在任何时候获取到当前的请求对象, 因此就可以拿到当前的用户信息
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    public static User getCurrentUser() {
        return (User) getCurrentRequest().getSession().getAttribute("user");
    }
}
